package com.rivulet.foreign.tools;

import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * {一句话形容此class功能}
 *
 * @author xujiac
 * @create 2021/7/26
 * @since 1.0.0
 */
public final class TimeTool {
    public final static Date StringToDate(String time) {
        return null;
    }

    public final static String DateToString(Date time) {
        if(time==null){
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        return simpleDateFormat.format(time);
    }

    public final static String DateToStringYM(Date time) {
        if(time==null){
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月");
        return simpleDateFormat.format(time);
    }

    public final static String DateToStringYMDTHM(Date time) {
        if(time==null){
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        return simpleDateFormat.format(time);
    }
    public final static Date StringToDate(String time,String type){
        if(type==null){
            return null;
        }
        if(type.equals("DateNumber")){
            Calendar c=new GregorianCalendar(1900,0,0);
            return DateUtils.addDays(c.getTime(), Integer.parseInt(time));
        }
        SimpleDateFormat format=new SimpleDateFormat(type);
        Date date=null;
        try {
            date=format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public final static String GetTimeType(String time){
        String [] years={"月","日"};
        String point=".";
        String result=null;
        if(StringTurnTime(time)){
            return "DateNumber";
        }
        if(time.contains(years[0])){
            if(time.contains(years[1])){
                result="yyyy年MM月dd日";
            }
            else {
                result="yyyy年MM月";
            }
        }
        if(time.contains(point)){
             String [] number=time.split("\\"+point);
             if(number.length==2){
                 result="yyyy.MM";
             }
             if(number.length==3){
                 result="yyyy.MM.dd";
             }
        }
        return result;
    }
    public final static boolean StringTurnTime(String time){
        try {
            Integer number=Integer.valueOf(time);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
